package com.example.petgrowing;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity2 extends AppCompatActivity {
    public String TAG = "home-petgrowing";
    public ImageView petImage;
    public Button feedButton;
    public Button playButton;
    public TextView coinText;
    public TextView HappinessText;
    public TextView SatisfyText;
    public TextView petLevelText;
    public Button shopButton;
    public Button workButton;
    public Button readButton;
    public int petLevel = 1;
    public int petSatisfy = 0;
    public int petHappiness = 0;
    public double coins = 200;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        petImage = findViewById(R.id.pet_image);
        feedButton = findViewById(R.id.feed_button);
        playButton = findViewById(R.id.play_button);
        readButton = findViewById(R.id.read_btn);
        coinText = findViewById(R.id.coin_text);
        HappinessText = findViewById(R.id.happiness_value);
        SatisfyText = findViewById(R.id.satisfy_value);
        shopButton = findViewById(R.id.shop_button);
        workButton = findViewById(R.id.workbutton);
        petLevelText = findViewById(R.id.pet_level);
        coinText.setText("金币: " + coins);
        HappinessText.setText("Happiness value:" + petHappiness);
        SatisfyText.setText("Satisfy value:"+petSatisfy);
        petLevelText.setText("Pet level:"+petLevel);
        updateCoinText();

        feedButton.setOnClickListener(view -> feedPet());
        playButton.setOnClickListener(view -> playWithPet());
        shopButton.setOnClickListener(view -> shopforPet());
        workButton.setOnClickListener(view -> workPet());
        readButton.setOnClickListener(view -> readPet());
    }
    public void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
            double newCoins = data.getDoubleExtra("Money",0);
            double growSatisfy = (data.getDoubleExtra("GrowSatisfy",0))*2;
            petSatisfy += growSatisfy;
            updatePetStatus();
            updatePetImage();
            Log.i(TAG, String.valueOf(newCoins)+"传回新余额值");
            Log.i(TAG, String.valueOf(growSatisfy)+"传回新购买的satisfy值");
            coins = newCoins;
            updateCoinText();
            HappinessText.setText("Happiness value:" + petHappiness);
            SatisfyText.setText("Satisfy value:"+petSatisfy);
            petLevelText.setText("Pet level:"+petLevel);
    }

    public void feedPet() {
        if (coins >= 10) {
            coins -= 10;
            petSatisfy += 10;
            updatePetStatus();
            updateCoinText();
            HappinessText.setText("Happiness value:" + petHappiness);
            SatisfyText.setText("Satisfy value:"+petSatisfy);
            petLevelText.setText("Pet level:"+petLevel);
        } else {
            Toast.makeText(this, "金币不足", Toast.LENGTH_SHORT).show();
        }
    }

    public void playWithPet() {
        if (coins >= 10) {
            coins -= 10;
            petHappiness += 10;
            updatePetStatus();
            updateCoinText();
            HappinessText.setText("Happiness value:" + petHappiness);
            SatisfyText.setText("Satisfy value:"+petSatisfy);
            petLevelText.setText("Pet level:"+petLevel);
        } else {
            Toast.makeText(this, "金币不足", Toast.LENGTH_SHORT).show();
        }
    }

    public void shopforPet(){
        Double money = Double.valueOf(coins);
        Log.i(TAG, String.valueOf(money));
        Intent goshop = new Intent(this,MainActivity3.class);
        goshop.putExtra("Money",money);
        startActivityForResult(goshop,1);
    }

    public void workPet(){
        if(petHappiness>1&&petSatisfy>1&&petLevel>1){
            coins +=5;
            petHappiness -=1;
            petSatisfy -= 1;
            updatePetStatus();
            updateCoinText();
            HappinessText.setText("Happiness value:" + petHappiness);
            SatisfyText.setText("Satisfy value:"+petSatisfy);
            petLevelText.setText("Pet level:"+petLevel);
        } else {
            Toast.makeText(this, "等级不够/happiness value 太低了// satisfy value太低了", Toast.LENGTH_SHORT).show();
        }
    }

    public void readPet(){
        Intent intent = new Intent(this,MainActivity4.class);
        startActivity(intent);
     }
    public void updatePetStatus() {
        if (petSatisfy >= 50 || petHappiness >= 50) {
            if(petLevel<4){
                petLevel++;
            } else if (petLevel==4) {
                petLevel = 4;
            }
            petSatisfy =0;
            petHappiness =0;
            updatePetImage();
            if(petLevel<4){
                Toast.makeText(this, "恭喜您升级到了"+petLevel+"级", Toast.LENGTH_SHORT).show();

            }else{
                Toast.makeText(this, "恭喜您已经满级了", Toast.LENGTH_SHORT).show();
            }
        } else {
            updatePetImage();
        }
    }

    public void updatePetImage() {
        switch (petLevel) {
            case 1:
                if (petSatisfy >= 50 || petHappiness >= 50) {
                    petImage.setImageResource(R.drawable.pet2);
                } else {
                    petImage.setImageResource(R.drawable.pet1);
                }
                break;
            case 2:
                if (petSatisfy >= 50 || petHappiness >= 50) {
                    petImage.setImageResource(R.drawable.pet3);
                } else {
                    petImage.setImageResource(R.drawable.pet2);
                }
                break;
            case 3:
                if (petSatisfy >= 50 || petHappiness >= 50) {
                    petImage.setImageResource(R.drawable.pet4);
                } else {
                    petImage.setImageResource(R.drawable.pet3);
                }
                break;
            default:
                petImage.setImageResource(R.drawable.pet4);
                break;
        }
    }
    public void updateCoinText() {
        coinText.setText("金币: " + coins);
    }
}